<template>
  <!-- 排班 -->
  <section>
    <el-form :inline="true" @submit.native.prevent="getData(-1)">
      <div class="form-tool">
        <div>
          <el-form-item>
            <deptSearch v-model="form.deptId" @change="onChangeDept"></deptSearch>
          </el-form-item>
          <el-form-item>
            <doctorSearch ref="doctorSearch" v-model="form.doctorId" :deptId="form.deptId"></doctorSearch>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" native-type="submit" circle icon="el-icon-search"></el-button>
          </el-form-item>
        </div>
        <div>
          <el-form-item>
            <el-button :disabled="!qxBtns.type" @click="$router.push('paibantype')">排班类型</el-button>
          </el-form-item>
        </div>
      </div>
    </el-form>
    <table border="0" width="100%">
      <tr>
        <td>
          <el-button-group>
            <el-button @click="onSwitch(0)">上周</el-button>
            <el-button @click="onSwitch(1)">下周</el-button>
          </el-button-group>
        </td>
        <td style="text-align:center;font-size:18px;">
          <span v-if="weekArray.length>0">
            {{weekArray[0].intactDateDay}}&nbsp;&nbsp;&nbsp;-&nbsp;&nbsp;&nbsp;{{weekArray[6].intactDateDay}}
          </span>
        </td>
        <td style="text-align:right;padding-right:8px;">
          <el-button type="primary" :disabled="!qxBtns.copy||copyShow" @click="onCopyWeek">复制上周</el-button>
        </td>
      </tr>
    </table>
    <br>
    <table class="table-box">
      <thead>
        <tr>
          <th></th>
          <th>人员</th>
          <th v-for="(item,index) in weekArray" :key="index">{{item.week}}&nbsp;{{item.dateDay}}</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(item,index) in tableList" :key="index">
          <td> <img v-if="item.head" :src="item.head|imageUrl" alt="" class="head-img" style="width:40px;height:40px;"></td>
          <td>
            <div style="min-width:100px">
              {{item.trueName}}
            </div>
          </td>
          <td v-for="(item2,index2) in item.scheduleDoctors" :key="index2"> 
            <el-input :value="item2|toPaiBanStr" placeholder="请选择" clearable :disabled="item2.dateDay|isCurDay" @focus="setScheduleSchedule($event,item2,item)" @clear="onDel(item2)"></el-input>
          </td>
        </tr>
      </tbody>
    </table>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
      <div class="error-color p-sm">请选择科室或医查询</div>
    </div>
    <br>
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <selectPlan v-model="dialogPaiBan" :dialogPaiBanObj="dialogPaiBanObj" @success="getData"></selectPlan>
  </section>
</template>
<script>
export default {
  name: "paiban",
  filters: {
    isCurDay(value) {
      let date = new Date(value);
      let newDate = new Date();
      let str = `${date.getFullYear()}-${date.getMonth()}-${date.getDate()}`;
      let str1 = `${newDate.getFullYear()}-${newDate.getMonth()}-${newDate.getDate()}`;
      if (str == str1) {
        return false;
      } else if (date.getTime() > newDate.getTime()) {
        return false;
      } else return true;
    },
    toPaiBanStr(value) {
      let str = "";
      if (value.typeTitle) {
        str = value.typeTitle;
        if (value.hpDiagnosisRoom) str += "--" + value.hpDiagnosisRoom.roomName;
      }
      return str;
    }
  },
  components: {
    selectPlan: () => import("./_com/selectPlan.vue"),
    deptSearch: () => import("@/pages/_com/deptSearch.vue"),
    doctorSearch: () => import("@/pages/_com/doctorSearch.vue")
  },
  data() {
    return {
      dialogPaiBan: false,
      dialogPaiBanObj: {
        deptId: "",
        doctorId: "",
        timeStr: "",
        obj: null
      },
      curDate: new Date(),
      form: {
        hpId: "",
        deptId: "",
        doctorId: "",
        startDate: "",
        type: 1,
        days: 7
      },
      copyShow: true, //是否禁用
      pager: { pageSize: 15, pageNum: 1, total: 0 },
      tableList: [],
      weekArray: [], //周
      qxBtns: {}
    };
  },
  mounted() {
    let th = this;
    th.form.hpId = th.adminUser.hpId;
    let week = th.curDate.getDay();
    th.curDate.setDate(th.curDate.getDate() + (1 - week));
    th.getQX();
  },
  methods: {
    onChangeDept() {
      let th = this;
      th.form.doctorId = "";
      th.$nextTick(function() {
        th.$refs["doctorSearch"].getData();
      });
    },
    //设置排班
    setScheduleSchedule(value, item1, item2) {
      let th = this;
      if (!th.qxBtns.plan) return;
      th.dialogPaiBanObj = {
        deptId: th.form.deptId,
        doctorId: item2.id,
        timeStr: item1.dateDay,
        obj: item1
      };
      th.dialogPaiBan = true;
    },
    getData(type) {
      let th = this;
      if (!th.form.deptId) {
        th.$message.error("请先选择科室");
        return;
      }
      th.form.startDate = th.$utils.FormatTime(
        "yyyy-MM-dd",
        th.curDate.getTime()
      );
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      let j = {};
      Object.assign(j, th.pager);
      Object.assign(j, th.form);
      th.showLoading();
      th.$api.scheduleList(j).then(res => {
        th.showLoading(false);
        if (res.executed) {
          th.tableList = res.dataBody.doctorScheduleInfos.list;
          th.weekArray = res.dataBody.daysInfos;
          th.pager.total = res.dataBody.doctorScheduleInfos.total;
        }
        //判断是否显示复制按钮
        let date = new Date().getTime();
        let date2 = new Date(th.weekArray[0].intactDateDay).getTime();
        if (date2 > date) {
          th.copyShow = false;
        } else {
          th.copyShow = true;
        }
      });
    },
    //删除排班
    onDel(obj) {
      let th = this;
      let j = {
        scheduleId: obj.id,
        doctorId: obj.doctorId,
        date: obj.dateDay
      };
      th.showLoading();
      th.$api
        .canCelschedule(j)
        .then(res => {
          th.showLoading(false);
          if (res.executed) {
            th.$message.success("取消排班成功");
            th.$set(obj, "typeTitle", "");
          } else {
            th.$message.error(res.message);
          }
        })
        .catch(res => {
          th.showLoading(false);
        });
    },
    //切换上下周
    onSwitch(type) {
      let th = this;
      if (th.tableList.length == 0) return;
      let date = new Date(th.curDate);
      if (type == 0) {
        //上
        date.setDate(date.getDate() - 7);
      } else {
        //下
        date.setDate(date.getDate() + 7);
      }
      th.curDate = date;
      th.getData();
    },
    //复制排班
    onCopyWeek() {
      let th = this;
      if (!th.qxBtns.plan) {
        th.$message.warning("暂无权限");
        return;
      }
      if (th.tableList.length == 0) return;
      th.$confirm(`确定复制上周，如果本周有排班将会被覆盖`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          let j = {
            hpId: th.form.hpId,
            deptId: th.form.deptId,
            doctorId: th.form.doctorId,
            type: th.form.type,
            timeStr: th.$utils.FormatTime("yyyy-MM-dd", th.curDate.getTime())
          };
          th.showLoading();
          th.$api.scheduleCopy(j).then(res => {
            if (res.executed) {
              th.$message.success("操作成功");
              th.getData();
            } else {
              th.$message.error(res.message);
            }
            th.showLoading(false);
          });
        })
        .catch(() => {});
    }
  },
  watch: {
    "form.deptId"(n, o) {
      this.tableList = [];
    },
    "form.type"(n, o) {
      this.tableList = [];
    }
  }
};
</script>
